package com.ruoyi.kjcg.qygfgb.service.impl;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.infoHiComment.domain.InfoHiComment;
import com.ruoyi.infoHiComment.mapper.InfoHiCommentMapper;
import com.ruoyi.infoRunTaskData.service.IInfoRunTaskDataService;
import com.ruoyi.kjcg.qygf.domain.Qygf;
import com.ruoyi.kjcg.qygf.mapper.QygfMapper;
import com.ruoyi.kjcg.qygf.service.IQygfService;
import com.ruoyi.kjcg.qygfgb.domain.Qygfgb;
import com.ruoyi.kjcg.qygfgb.domain.QygfgbGffblb;
import com.ruoyi.kjcg.qygfgb.mapper.QygfgbMapper;
import com.ruoyi.kjcg.qygfgb.service.IQygfgbService;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 企业工法公布Service业务层处理
 *
 * @author huacai
 * @date 2024-11-05
 */
@Service
public class QygfgbServiceImpl implements IQygfgbService {
    @Resource
    private QygfgbMapper qygfgbMapper;

    @Resource
    private SqlSessionFactory sqlSessionFactory;

    @Resource
    private IInfoRunTaskDataService iInfoRunTaskDataService;

    // 企业工法申报Service
    @Resource
    private IQygfService iQygfService;

    // 企业工法申报mapper
    @Resource
    private QygfMapper qygfMapper;

    @Resource
    private InfoHiCommentMapper infoHiCommentMapper;

    /**
     * 查询企业工法公布
     *
     * @param qygfgbId 企业工法公布主键
     * @return 企业工法公布
     */
    @Override
    public Qygfgb selectQygfgbByQygfgbId(Integer qygfgbId) {
        return qygfgbMapper.selectQygfgbByQygfgbId(qygfgbId);
    }

    /**
     * 查询企业工法公布列表
     *
     * @param qygfgb 企业工法公布
     * @return 企业工法公布
     */
    @Override
    public List<Qygfgb> selectQygfgbList(Qygfgb qygfgb) {
        return qygfgbMapper.selectQygfgbList(qygfgb);
    }

    /**
     * 企业工法公布
     *
     * @param qygfgb 企业工法公布
     * @return 结果
     */
    @Transactional
    @Override
    public int insertQygfgb(Qygfgb qygfgb) {
        qygfgb.setCreateTime(DateUtils.getNowDate());

        //工法发布列表的信息
        List<QygfgbGffblb> qygfgbGffblbList = qygfgb.getQygfgbGffblbList();
        for (QygfgbGffblb qygfgbGffblb : qygfgbGffblbList) {
            //修改业务数据状态为完成 5已发布
            iInfoRunTaskDataService.updateStatus(qygfgbGffblb.getQygfId(), 12, 5);

            //在追踪过程中加入流程记录信息
            InfoHiComment infoHiComment = new InfoHiComment();
            infoHiComment.setCreateTime(new Date());//审批时间
            infoHiComment.setCreateName(SecurityUtils.getLoginUser().getUser().getNickName()); //用户昵称
            infoHiComment.setTaskName("公布工法");
            infoHiComment.setMessage(qygfgb.getShYj()); //审核意见
            //拿到该工法的processInstanceId
            Qygf qygf = iQygfService.selectQygfByQygfId(qygfgbGffblb.getQygfId());
            //在记录表中插入拿到该工法的processInstanceId
            infoHiComment.setProcessInstanceId(qygf.getProcessInstanceId());
            //插入审批记录
            infoHiCommentMapper.insertInfoHiComment(infoHiComment);

        }
        int rows = qygfgbMapper.insertQygfgb(qygfgb);
        insertQygfgbGffblb(qygfgb);
        return rows;
    }

    /**
     * 企业工法评审未通过
     *
     * @param qygfgb
     * @return
     */
    @Override
    public int insertPsWTg(Qygfgb qygfgb) {
        qygfgb.setCreateTime(DateUtils.getNowDate());

        //工法发布列表的信息
        List<QygfgbGffblb> qygfgbGffblbList = qygfgb.getQygfgbGffblbList();
        for (QygfgbGffblb qygfgbGffblb : qygfgbGffblbList) {
            //修改业务数据状态为完成 6评审未通过
            iInfoRunTaskDataService.updateStatus(qygfgbGffblb.getQygfId(), 12, 6);

            //在追踪过程中加入流程记录信息
            InfoHiComment infoHiComment = new InfoHiComment();
            infoHiComment.setCreateTime(new Date());//审批时间
            infoHiComment.setCreateName(SecurityUtils.getLoginUser().getUser().getNickName()); //用户昵称
            infoHiComment.setTaskName("评审未通过");
            infoHiComment.setMessage(qygfgb.getShYj()); //审核意见
            //拿到该工法的processInstanceId
            Qygf qygf = iQygfService.selectQygfByQygfId(qygfgbGffblb.getQygfId());
            //在记录表中插入拿到该工法的processInstanceId
            infoHiComment.setProcessInstanceId(qygf.getProcessInstanceId());
            //插入审批记录
            infoHiCommentMapper.insertInfoHiComment(infoHiComment);

        }
        int rows = qygfgbMapper.insertQygfgb(qygfgb);
        insertQygfgbGffblbWtg(qygfgb);
        return rows;
    }

    /**
     * 批量新增企业工法公布
     *
     * @param qygfgbs 企业工法公布List
     * @return 结果
     */
    @Override
    public int batchInsertQygfgb(List<Qygfgb> qygfgbs) {
        SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH, false);
        int count = 0;
        if (!CollectionUtils.isEmpty(qygfgbs)) {
            try {
                for (int i = 0; i < qygfgbs.size(); i++) {
                    int row = qygfgbMapper.insertQygfgb(qygfgbs.get(i));
                    // 防止内存溢出，每100次提交一次,并清除缓存
                    boolean bool = (i > 0 && i % 100 == 0) || i == qygfgbs.size() - 1;
                    if (bool) {
                        sqlSession.commit();
                        sqlSession.clearCache();
                    }
                    count = i + 1;
                }
            } catch (Exception e) {
                e.printStackTrace();
                // 没有提交的数据可以回滚
                sqlSession.rollback();
            } finally {
                sqlSession.close();
                return count;
            }
        }
        return count;
    }

    /**
     * 修改企业工法公布
     *
     * @param qygfgb 企业工法公布
     * @return 结果
     */
    @Transactional
    @Override
    public int updateQygfgb(Qygfgb qygfgb) {
        qygfgb.setUpdateTime(DateUtils.getNowDate());
        qygfgbMapper.deleteQygfgbGffblbByQygfgbId(qygfgb.getQygfgbId());
        insertQygfgbGffblbWtg(qygfgb);
        return qygfgbMapper.updateQygfgb(qygfgb);
    }

    /**
     * 批量删除企业工法公布
     *
     * @param qygfgbIds 需要删除的企业工法公布主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteQygfgbByQygfgbIds(Integer[] qygfgbIds) {
        qygfgbMapper.deleteQygfgbGffblbByQygfgbIds(qygfgbIds);
        return qygfgbMapper.deleteQygfgbByQygfgbIds(qygfgbIds);
    }

    /**
     * 删除企业工法公布信息
     *
     * @param qygfgbId 企业工法公布主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteQygfgbByQygfgbId(Integer qygfgbId) {
        qygfgbMapper.deleteQygfgbGffblbByQygfgbId(qygfgbId);
        return qygfgbMapper.deleteQygfgbByQygfgbId(qygfgbId);
    }

    /**
     * 新增工法发布列信息 (公布时使用)
     *
     * @param qygfgb 企业工法公布对象
     */
    public void insertQygfgbGffblb(Qygfgb qygfgb) {
        List<QygfgbGffblb> qygfgbGffblbList = qygfgb.getQygfgbGffblbList();
        Integer qygfgbId = qygfgb.getQygfgbId();
        if (StringUtils.isNotNull(qygfgbGffblbList)) {
            List<QygfgbGffblb> list = new ArrayList<QygfgbGffblb>();
            for (QygfgbGffblb qygfgbGffblb : qygfgbGffblbList) {
                qygfgbGffblb.setQygfgbId(qygfgbId);
                list.add(qygfgbGffblb);

                qygfgbMapper.insertQygfgbGffblb(qygfgbGffblb);

                Qygf qygf = iQygfService.selectQygfByQygfId(qygfgbGffblb.getQygfId());
                Calendar calendar = Calendar.getInstance();

                // 获取当前年
//                String year = String.valueOf(calendar.get(Calendar.YEAR));
                //获取那一年有几条工法
                String gfLengthByYear = qygfgbMapper.selectGfLengthByYear(qygfgbGffblb.getNd());
                int number = Integer.parseInt(gfLengthByYear); // 将字符串转换为整数
                String i = String.format("%03d", number); // 格式化回带有前导零的字符串
                //插入工法编号
                qygf.setGfBh("WJGF-" + i + "-" + qygfgbGffblb.getNd());
                qygfMapper.updateQygf(qygf);


            }
//            if (list.size() > 0) {
//                qygfgbMapper.batchQygfgbGffblb(list);
//            }
        }
    }

    /**
     * 新增工法发布列信息 (未通过时使用)
     *
     * @param qygfgb 企业工法公布对象
     */
    public void insertQygfgbGffblbWtg(Qygfgb qygfgb) {
        List<QygfgbGffblb> qygfgbGffblbList = qygfgb.getQygfgbGffblbList();
        Integer qygfgbId = qygfgb.getQygfgbId();
        if (StringUtils.isNotNull(qygfgbGffblbList)) {
            List<QygfgbGffblb> list = new ArrayList<QygfgbGffblb>();
            for (QygfgbGffblb qygfgbGffblb : qygfgbGffblbList) {
                qygfgbGffblb.setQygfgbId(qygfgbId);
                list.add(qygfgbGffblb);
            }
            if (list.size() > 0) {
                qygfgbMapper.batchQygfgbGffblb(list);
            }
        }
    }

}
